//
//  WebCashViewController.m
//  myselfTest
//
//  Created by Lucifer on 15-5-21.
//  Copyright (c) 2015年 xuxzhou. All rights reserved.
//

#import "WebCashViewController.h"
#import "UIImageView+WebCache.h"

#import "MyPictureCollectionViewCell.h"
#import "MyPictureCollectionReusableView.h"
@interface WebCashViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic,strong)NSArray * dataArray;
@property(nonatomic,strong)UICollectionView * collectionView;
@property(nonatomic,strong)UIButton * clearButton;
@property (nonatomic, strong) MyPictureCollectionReusableView* headerView;


@end

@implementation WebCashViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _dataArray = @[@"http://www.caos.bg.it/immagini/albero_3.jpg",
               @"http://1.bp.blogspot.com/-AWQC0Kw9q_Q/Uq8uHsrQkpI/AAAAAAAAFVo/GHOKcf7nrXw/s640/cars.png",
               @"http://www.german-concept-cars.com/wp-content/uploads/2010/05/German-concept-Cars-Home1.jpg",
               @"https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcSOUxre9MKwPxFwb89wO33V4PzzCFnZ5ttP6tWmP5YQzRtk_40h",
               @"http://assets.esquire.co.uk/images/uploads/fourbythree/_540_43/l_236-four-of-the-best-american-muscle-cars-2.jpg",
               @"http://sportscommunity.com.au/wp-content/uploads/2013/01/sports-collage.jpg",
               @"http://i.telegraph.co.uk/multimedia/archive/01806/earth_1806334c.jpg",
               @"https://battlingthedemonswithin.files.wordpress.com/2013/09/earth-cd321c592915ddb9165e20d1053edce9ee78cd3b-s6-c30.jpg",
               @"http://upload.wikimedia.org/wikipedia/commons/c/cc/2008_Ducati_848_Showroom.jpg",
               @"http://kickstart.bikeexif.com/wp-content/uploads/2014/01/ducati-999.jpg",
               @"http://kickstart.bikeexif.com/wp-content/uploads/2012/09/ducati-pantah-2.jpg",
               @"http://static.derapate.it/derapate/fotogallery/625X0/3775/ducati-999.jpg",
               @"http://siliconangle.com/files/2012/03/github_logo.jpg",
               @"https://octodex.github.com/images/octobiwan.jpg",
               @"https://octodex.github.com/images/murakamicat.png",
               @"http://tinyurl.com/q7xc48y"];
    UICollectionViewFlowLayout* flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 1;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flowLayout.headerReferenceSize = CGSizeMake(CGRectGetWidth([[UIScreen mainScreen] bounds]), ( CGFloat )CGRectGetWidth( self.view.frame ) * 0.5);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 00, CGRectGetWidth(self.view.frame), CGRectGetHeight([[UIScreen mainScreen] bounds]) - 64 - 49) collectionViewLayout:flowLayout];
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:[MyPictureCollectionViewCell class] forCellWithReuseIdentifier:@"collectionCell"];
    [self.collectionView registerClass:[MyPictureCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerIdentifier"];
    [self.view addSubview:self.collectionView];
    
    
    _clearButton=[UIButton buttonWithType:UIButtonTypeCustom];
    _clearButton.frame=CGRectMake(0, 0, 44, 44);
    [_clearButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [_clearButton setTitle:@"清理" forState:UIControlStateNormal];
    UIBarButtonItem * leftButton=[[UIBarButtonItem alloc]initWithCustomView:_clearButton];
    [self.navigationItem setRightBarButtonItem:leftButton];
    [_clearButton addTarget:self action:@selector(buttonclicked) forControlEvents:UIControlEventTouchUpInside];

}

//清理图片缓存
-(void)buttonclicked{
    NSLog(@"%@",NSHomeDirectory());

    NSString *imageDir = [NSString stringWithFormat:@"%@/Library/Caches/%@", NSHomeDirectory(), @"com.hackemist.SDWebImageCache.default"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    [fileManager removeItemAtPath:imageDir error:nil];
    [_collectionView reloadData];

}

#pragma mark - collection view delegate

- (NSInteger)collectionView:(UICollectionView*)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath
{
    MyPictureCollectionViewCell* cell = (MyPictureCollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"collectionCell" forIndexPath:indexPath];
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:_dataArray[indexPath.row]] placeholderImage:[UIImage imageNamed:@"gbp"]];
    cell.indexPath = indexPath;
    return cell;
}

- (UICollectionReusableView*)collectionView:(UICollectionView*)collectionView viewForSupplementaryElementOfKind:(NSString*)kind atIndexPath:(NSIndexPath*)indexPath
{
    if (kind == UICollectionElementKindSectionHeader) {
        _headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headerIdentifier" forIndexPath:indexPath];
        _headerView.imageUrls=_dataArray;
        return _headerView;
    }
    return nil;
}

- (CGSize)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath*)indexPath
{
    CGFloat width = CGRectGetWidth(self.view.frame) / 3;
    CGFloat width1 = (CGFloat)(ceil(width * 2 - 1)) / 2;
    CGFloat width2 = (CGFloat)ceil(width * 2)/2;
    return CGSizeMake(width1, width2);
}

- (void)collectionView:(UICollectionView*)collectionView didSelectItemAtIndexPath:(NSIndexPath*)indexPath
{
    
}

- (void)didReceiveMemoryWarning

{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
